package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartServlet;

    @GetMapping("/list")
    public R<List<ShoppingCart>> listR(){

        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        lqw.orderByAsc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = shoppingCartServlet.list(lqw);

        return R.success(list);
    }

    @PostMapping("/add")
    public R<ShoppingCart> stringR(@RequestBody ShoppingCart shoppingCart){

        log.info("{}",shoppingCart);
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,shoppingCart.getUserId());

        if (shoppingCart.getDishId()!=null){

            lqw.eq(ShoppingCart::getDishId,shoppingCart.getDishId());

        }else {
            lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        ShoppingCart one = shoppingCartServlet.getOne(lqw);

        if (one!=null){
            Integer number = one.getNumber();
            one.setNumber(number+1);
            shoppingCartServlet.updateById(one);
        }else {

            shoppingCart.setNumber(1);
            shoppingCartServlet.save(shoppingCart);
            one = shoppingCart;
        }


        return R.success(one);
    }

    @DeleteMapping("/clean")
    public R<String> deleteR(){

        shoppingCartServlet.remove(null);
        //LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
        //shoppingCartServlet.remove(lqw);

        return R.success("删除成功");
    }

    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){

        log.info("{},{}",shoppingCart.getDishId(),shoppingCart.getSetmealId());

        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();
        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
        if (dishId!=null){
            lqw.eq(ShoppingCart::getDishId,dishId);
        }
        if (setmealId!=null){
            lqw.eq(ShoppingCart::getSetmealId,setmealId);
        }
        ShoppingCart one = shoppingCartServlet.getOne(lqw);
        Integer number = one.getNumber();
        if (number>1){

            one.setNumber(number-1);

        }else {
            one.setNumber(0);
        }
        shoppingCartServlet.updateById(one);
        return R.success("成功!!!");
    }

}
